<?php
/**
 * Created by JetBrains PhpStorm.
 * User: GMC
 * Date: 10/3/13
 * Time: 2:20 PM
 * To change this template use File | Settings | File Templates.
 */

class WidgetTopBranch extends CWidget {
    public $shops;

    public function init() {
        $top_branch = Yii::app()->cache->get('top_branch');
        if ($top_branch == null || count($top_branch) == 0) {
            $shops = Ads::model()->findAll(array(
                'select' => 'ads_id, ads_name, description, url, image_path',
                'condition' => 'position = :position',
                'params' => array(':position' => 'top-thuonghieu')
            ));

            $models = array();
            $result = array();
            $num = 1;
            $index = 0;
            foreach ($shops as $key => $item) {
                array_push($result, array(
                    'id' => $item->ads_id,
                    'name' => $item->ads_name,
                    'picture' => TreTodayUrl::getPictureUrl($item->image_path),
                    'description' => $item->description,
                    'url' => $item->url,
                ));
                if ($num == 8 || $key == count($shops) - 1) {
                    $models[$index] = $result;
                    $result = array();
                    $index++;
                    $num = 1;
                } else {
                    $num++;
                }
            }

            Yii::app()->cache->set('top_branch', $models, 'top_branch_time');
            $top_branch = Yii::app()->cache->get('top_branch');
        }
        $this->shops = $top_branch;
    }

    public function run() {
        return CWidget::render('widgetTopBranch', array('model' => $this->shops));
    }
}